﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace LibarayMS
{
    public partial class Frm_store_info : Form
    {
        protected int srid;
        private string mname;
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        public Frm_store_info(string mname)
        {
            this.mname = mname;
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            returnEvent();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        private void Frm_store_info_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            cb_state.SelectedIndex = 1;
            bind_store_info();
        }

        protected void clearTextBox()
        {
            txt_head.Text = "";
            cb_state.Text = "";
            label1.Text = "ISBN:";
            lbl_status.Text = "添加";
            txt_head.Enabled = true;
        }

        protected void bind_store_info()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "select SRID,SID,ISBN,BName,state,warehousing_date from book_store_info a,book_info b where a.BRID=b.BRID;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["SID"].ToString());
                myitem.SubItems.Add(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                if (dr["state"].ToString() == "0")
                {
                    myitem.SubItems.Add("借出");
                }
                else if (dr["state"].ToString() == "1")
                {
                    myitem.SubItems.Add("在库");
                }
                else
                {
                    myitem.SubItems.Add("损坏");
                }
                myitem.SubItems.Add(Convert.ToDateTime(dr["warehousing_date"]).ToString("yyyy-MM-dd"));
                //隐藏列SRID
                myitem.SubItems.Add(dr["SRID"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (cb_state.Text == "")
            {
                lbl_note.Text = "请选择状态！";
                lbl_note.ForeColor = Color.Red;
                cb_state.Focus();
            }
            else if (lbl_status.Text == "添加")
            {
                if (txt_head.Text == "")
                {
                    lbl_note.Text = "请输入ISBN！";
                    lbl_note.ForeColor = Color.Red;
                    txt_head.Focus();
                }
                else
                {
                    //添加图书馆藏信息
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("addStore", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    //定义输入函数
                    cmd.Parameters.AddWithValue("isbn_", txt_head.Text.Trim());
                    cmd.Parameters.AddWithValue("state_", cb_state.Text.Trim()=="借出"?0:1);
                    cmd.Parameters.AddWithValue("whtime_", dt_whtime.Value.ToString("yyyy-MM-dd"));
                    //定义输出函数
                    MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                    result.Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    if (result.Value.ToString() == "1")
                    {
                        lbl_note.Text = "恭喜你，添加成功！";
                        lbl_note.ForeColor = Color.Blue;
                        //trans.Commit();
                        //清空输入框
                        clearTextBox();
                        bind_store_info();
                    }
                    else
                    {
                        lbl_note.Text = "对不起，添加失败！";
                        lbl_note.ForeColor = Color.Red;
                    }
                    conn.Close();
                }
            }
            else
            {
                //更新图书馆藏信息
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("updateStore", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                //定义输入函数
                cmd.Parameters.AddWithValue("srid_", srid);
                cmd.Parameters.AddWithValue("state_", cb_state.Text.Trim() == "借出" ? 0 : 1);
                cmd.Parameters.AddWithValue("whtime_", dt_whtime.Value.ToString("yyyy-MM-dd"));
                //定义输出函数
                MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                result.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                if (result.Value.ToString() == "1")
                {
                    lbl_note.Text = "恭喜你，修改成功！";
                    lbl_note.ForeColor = Color.Blue;
                    //trans.Commit();
                    //清空输入框
                    clearTextBox();
                    bind_store_info();
                }
                else
                {
                    lbl_note.Text = "对不起，修改失败！";
                    lbl_note.ForeColor = Color.Red;
                }
                conn.Close();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                txt_head.Enabled = false;
                label1.Text = "图书馆藏编号：";
                ListViewItem myitem = listView1.SelectedItems[0];
                txt_head.Text = myitem.SubItems[0].Text;
                if (myitem.SubItems[3].Text == "在库")
                {
                    cb_state.SelectedIndex = 1;
                }
                else if (myitem.SubItems[3].Text == "借出")
                {
                    cb_state.SelectedIndex = 0;
                }
                else
                {
                    cb_state.SelectedIndex = 2;
                }
                dt_whtime.Value= DateTime.ParseExact(myitem.SubItems[4].Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                lbl_status.Text = "修改";
                srid = int.Parse(myitem.SubItems[5].Text);
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            txt_head.Enabled = true;
            clearTextBox();
            lbl_note.Text = "";
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择要删除的书籍！");
            }
            else
            {
                DialogResult dResult = MessageBox.Show("确定要删除选择的书籍？", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dResult == DialogResult.Yes)
                {
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("deleteStore", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    //定义输入函数
                    cmd.Parameters.AddWithValue("srid_", srid);
                    cmd.Parameters.AddWithValue("state_", cb_state.Text.Trim() == "借出" ? 0 : 1);
                    //定义输出函数
                    MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                    result.Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    if (result.Value.ToString() == "1")
                    {
                        lbl_note.Text = "恭喜你，删除成功！";
                        lbl_note.ForeColor = Color.Blue;
                        //清空输入框
                        clearTextBox();
                        bind_store_info();
                    }
                    else
                    {
                        lbl_note.Text = "对不起，删除失败！";
                        lbl_note.ForeColor = Color.Red;
                    }
                    conn.Close();
                }
            }
        }
    }
}
